<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    :title="isEdit ? '编辑水库划界' : '新增水库划界'"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="default"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="划界编号" prop="boundaryNo">
            <el-input v-model="form.boundaryNo" placeholder="请输入划界编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="水库名称" prop="reservoirName">
            <el-input v-model="form.reservoirName" placeholder="请输入水库名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="划界类型" prop="boundaryType">
            <el-select v-model="form.boundaryType" placeholder="选择划界类型" style="width: 100%">
              <el-option label="管理范围" value="management" />
              <el-option label="保护范围" value="protection" />
              <el-option label="禁建范围" value="construction_ban" />
              <el-option label="限建范围" value="construction_limit" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="划界日期" prop="boundaryDate">
            <el-date-picker
              v-model="form.boundaryDate"
              type="date"
              placeholder="选择划界日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="划界面积" prop="boundaryArea">
            <el-input v-model="form.boundaryArea" placeholder="请输入划界面积(平方米)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="划界长度" prop="boundaryLength">
            <el-input v-model="form.boundaryLength" placeholder="请输入划界长度(米)" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="负责单位" prop="responsibleUnit">
            <el-input v-model="form.responsibleUnit" placeholder="请输入负责单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="responsiblePerson">
            <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="划界状态" prop="boundaryStatus">
            <el-select v-model="form.boundaryStatus" placeholder="选择划界状态" style="width: 100%">
              <el-option label="已完成" value="completed" />
              <el-option label="进行中" value="in_progress" />
              <el-option label="待开始" value="pending" />
              <el-option label="已暂停" value="suspended" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="地理坐标" prop="coordinates">
            <el-input
              v-model="form.coordinates"
              type="textarea"
              :rows="4"
              placeholder="请输入地理坐标信息"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="划界描述" prop="boundaryDescription">
            <el-input
              v-model="form.boundaryDescription"
              type="textarea"
              :rows="4"
              placeholder="请输入划界描述"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="法律依据" prop="legalBasis">
            <el-input
              v-model="form.legalBasis"
              type="textarea"
              :rows="3"
              placeholder="请输入法律依据"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">取消</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

interface Props {
  modelValue: boolean
  boundaryData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  boundaryData: null
})

const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()
const isEdit = ref(false)

const form = reactive({
  boundaryNo: '',
  reservoirName: '',
  boundaryType: '',
  boundaryDate: '',
  boundaryArea: '',
  boundaryLength: '',
  responsibleUnit: '',
  responsiblePerson: '',
  contactPhone: '',
  boundaryStatus: '',
  coordinates: '',
  boundaryDescription: '',
  legalBasis: '',
  remark: ''
})

const rules: FormRules = {
  boundaryNo: [
    { required: true, message: '请输入划界编号', trigger: 'blur' }
  ],
  reservoirName: [
    { required: true, message: '请输入水库名称', trigger: 'blur' }
  ],
  boundaryType: [
    { required: true, message: '请选择划界类型', trigger: 'change' }
  ],
  boundaryDate: [
    { required: true, message: '请选择划界日期', trigger: 'change' }
  ],
  responsibleUnit: [
    { required: true, message: '请输入负责单位', trigger: 'blur' }
  ],
  responsiblePerson: [
    { required: true, message: '请输入负责人', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  boundaryStatus: [
    { required: true, message: '请选择划界状态', trigger: 'change' }
  ]
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    boundaryNo: '',
    reservoirName: '',
    boundaryType: '',
    boundaryDate: '',
    boundaryArea: '',
    boundaryLength: '',
    responsibleUnit: '',
    responsiblePerson: '',
    contactPhone: '',
    boundaryStatus: '',
    coordinates: '',
    boundaryDescription: '',
    legalBasis: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    // 这里应该调用API保存数据
    console.log('提交表单数据:', form)
    
    ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
    emit('success')
    emit('update:modelValue', false)
  } catch (error) {
    ElMessage.error('请检查表单填写是否正确')
  }
}

// 监听弹窗打开，初始化表单数据
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    if (props.boundaryData) {
      isEdit.value = true
      Object.assign(form, props.boundaryData)
    } else {
      isEdit.value = false
      resetForm()
    }
  }
}, { immediate: true })
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>